
extension String {
    public func test(regex pattern: String) -> Bool {
        do {
            let regex = try NSRegularExpression(pattern: pattern, options: [])
            let range = NSRange(location: 0, length: self.utf16.count)
            
            let matches = regex.numberOfMatches(in: self, options: [], range: range)
            return matches > 0
        } catch {
            // 处理错误，你可以根据需要进行适当的处理，比如打印错误信息
            //            print("Error: \(error)")
            return false
        }
    }
    public func replace(regex pattern: String, with replacement: String) -> String {
        do {
            let regex = try NSRegularExpression(pattern: pattern, options: [])
            let range = NSRange(location: 0, length: self.utf16.count)
            
            return regex.stringByReplacingMatches(in: self, options: [], range: range, withTemplate: replacement)
        } catch {
            // 如果发生错误，返回原始字符串
            //            print("Error: \(error)")
            return self
        }
    }
}

